﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicLibrary.Model;
using MedicLibrary.Controler;

namespace MedicLibrary.View
{
    public partial class frmChiTietThuoc : Form
    {
        public String action;
        Thuoc ctT = new Thuoc();
        HoatChatThuoc HC = new HoatChatThuoc();
        
        public frmChiTietThuoc()
        {
            InitializeComponent();
            
        }
       

        private void frmChiTietThuoc_Load(object sender, EventArgs e)
        {

            if (action == "VIEW" || action == "EDIT") 
            {
                if( ctT.readThongTinThuoc(Session.IDThuoc) == false)
                    MessageBox.Show("Không đọc được thông tin Thuốc!");
                else 
                    bindData(); 
            }
            txtTenNhom.Focus();
        }

        public void applyAction(String ac)
        {
            this.action = ac;
            bool status = false;
            if (this.action == "EDIT" || this.action == "ADD")
            {
               
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                btnCopy.Enabled = false;
                btnHuy.Enabled = true;
                btnLuu.Enabled = true;
                status = true;
            }
            else if (this.action == "VIEW")
            {
                btnHuy.Enabled = false;
                btnLuu.Enabled = false;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnCopy.Enabled = true;
                status = false;
            }
            btnChonHangSX.Enabled = status;
            btnChonNhom.Enabled = status;
            btnHoatChat.Enabled = status;
            btnXoaHC.Enabled = status;
            txtTenThuoc.Enabled = status;
            txtMaThuoc.Enabled = status;
            txtQuocGia.Enabled = status;
            txtTenHangSX.Enabled = status;
            txtTenNhom.Enabled = status;
            rtxChiDinh.Enabled = status;
            rtxChongChiDinh.Enabled = status;
            rtxDuocDongHoc.Enabled = status;
            rtxDuocLucHoc.Enabled = status;
            rtxGhiChu.Enabled = status;
            rtxHamLuong.Enabled = status;
            rtxLLCD.Enabled = status;
            rtxLuuY.Enabled = status;
            rtxTacDungPhu.Enabled = status;
            rtxThanTrong.Enabled = status;
            rtxHinhThuc.Enabled = status;

            if (this.action == "ADD")
            {
                clearForm();
                btnHoatChat.Enabled = true;
                btnXoaHC.Enabled = false;
            }
        }

        public void clearForm()
        {
            txtMaThuoc.Text = "";
            txtTenThuoc.Text = "";
            txtQuocGia.Text = "";
            txtTenHangSX.Text = "";
            txtTenNhom.Text = "";
            rtxChiDinh.Text = "";
            rtxChongChiDinh.Text = "";
            rtxHamLuong.Text = "";
            rtxHinhThuc.Text = "";
            rtxLLCD.Text = "";
            rtxLuuY.Text = "";
            rtxTacDungPhu.Text = "";
            rtxThanTrong.Text = "";
            rtxGhiChu.Text = "";
            rtxDuocDongHoc.Text = "";
            rtxDuocLucHoc.Text = "";
        }

        public void bindData()
        {
            HC.ThuocID              = ctT.ID;
            txtMaThuoc.Text         = ctT.MaThuoc;
            txtTenThuoc.Text        = ctT.TenThuoc;
            txtQuocGia.Text         = ctT.QuocGia;
            txtTenHangSX.Text       = ctT.TenHangSX;
            txtTenNhom.Text         = ctT.TenNhom;
            rtxChiDinh.Text         = ctT.ChiDinh;
            rtxChongChiDinh.Text    = ctT.ChongChiDinh;
            rtxHamLuong.Text        = ctT.HamLuong;
            rtxHinhThuc.Text        = ctT.HinhThuc;
            rtxLLCD.Text            = ctT.LieuLuongCachDung;
            rtxLuuY.Text            = ctT.LuuY;
            rtxTacDungPhu.Text      = ctT.TacDungPhu;
            rtxThanTrong.Text       = ctT.ThanTrong;
            rtxGhiChu.Text          = ctT.GhiChu;
            rtxDuocDongHoc.Text     = ctT.DuocDongHoc;
            rtxDuocLucHoc.Text      = ctT.DuocLucHoc;
            dgvDSHoatChat.DataSource  = HoatChatThuoc.getDanhSachHoatChat(ctT.ID); 
        }

        public void setData()
        {
            if (action == "ADD")
                this.ctT.getMaxID();
            else if (action == "EDIT" || action == "VIEW")
                this.ctT.ID = Session.IDThuoc;
            this.ctT.MaThuoc = txtMaThuoc.Text;
            this.ctT.TenThuoc = txtTenThuoc.Text;
            this.ctT.QuocGia = txtQuocGia.Text;
            this.ctT.TenHangSX = txtTenHangSX.Text;
            this.ctT.TenNhom = txtTenNhom.Text;
            this.ctT.ChiDinh = rtxChiDinh.Text;
            this.ctT.ChongChiDinh = rtxChongChiDinh.Text;
            this.ctT.HamLuong = rtxHamLuong.Text;
            this.ctT.HinhThuc = rtxHinhThuc.Text;
            this.ctT.LieuLuongCachDung = rtxLLCD.Text;
            this.ctT.LuuY = rtxLuuY.Text;
            this.ctT.TacDungPhu = rtxTacDungPhu.Text;
            this.ctT.ThanTrong = rtxThanTrong.Text;
            this.ctT.GhiChu = rtxGhiChu.Text;
            this.ctT.DuocDongHoc = rtxDuocDongHoc.Text;
            this.ctT.DuocLucHoc = rtxDuocLucHoc.Text;  
                      
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            setData();
            if(action == "ADD")
                if (txtTenThuoc.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập tên thuốc!");
                    return;
                }
                else
                {
                    this.ctT.insertThuoc();
                    this.txtMaThuoc.Text = this.ctT.MaThuoc;
                    HC.ThuocID = ctT.ID;
                }
            else if(action == "EDIT")
                this.ctT.updateThuoc();
            this.applyAction("VIEW");
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (action == "ADD")
                this.Close();
            this.applyAction("VIEW");
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            applyAction("EDIT");
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn xóa Thuốc này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                setData();
                this.ctT.deleteThuoc();
                this.Close();
            }
        }

     

        private void btnSelectNhomCha_Click(object sender, EventArgs e)
        {
            View.frmDSNhomThuoc a = new View.frmDSNhomThuoc();
            a.getNC = new View.frmDSNhomThuoc.getNhomCha(setNhomCha);
            a.applyAction("SEARCH");
            a.ShowDialog();
        }

      
        private void cbxNhomCha_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void setNhomCha(int ID, String fname)
        {
         
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn copy Thuốc này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                setData();
                ctT.getMaxID();
                this.ctT.insertThuoc();
                Session.IDThuoc = ctT.ID;
                this.ctT.readThongTinThuoc(Session.IDThuoc);
                bindData();
            }
        }

        private void btnChonNhom_Click(object sender, EventArgs e)
        {
            frmDSNhomThuoc dsNhom = new frmDSNhomThuoc();
            dsNhom.getNC = new View.frmDSNhomThuoc.getNhomCha(setNhomThuoc);
            dsNhom.applyAction("SEARCH");
            dsNhom.ShowDialog();
        }

        private void btnChonHangSX_Click(object sender, EventArgs e)
        {
            frmDSHangSX dsNhom = new frmDSHangSX();
            dsNhom.getHSX = new View.frmDSHangSX.getHangSX(setHangSX);
            dsNhom.applyAction("SEARCH");
            dsNhom.ShowDialog();
        }

        private void btnHoatChat_Click(object sender, EventArgs e)
        {
            if (ctT.ID != 0)
            {
                frmDSHoatChat dsHC = new frmDSHoatChat();
                dsHC.getHC = new View.frmDSHoatChat.getHoatChat(setHC);
                dsHC.applyAction("SEARCH");
                dsHC.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn vui lòng lưu thuốc trước khi thay đổi hoạt chất");
            }
        }

        private void btnXoaHC_Click(object sender, EventArgs e)
        {
            if (this.dgvDSHoatChat.SelectedRows.Count != 0)
            {
                HC.ID = Int32.Parse(this.dgvDSHoatChat.SelectedRows[0].Cells["ID"].Value.ToString());
                MessageBox.Show(this.dgvDSHoatChat.SelectedRows[0].Cells["ID"].Value.ToString());
                HC.deleteHoatChat();
                dgvDSHoatChat.DataSource = HoatChatThuoc.getDanhSachHoatChat(ctT.ID);
            }
        }

        public void setNhomThuoc(int ID, String fname)
        {
            txtTenNhom.Text = fname;
            ctT.NhomThuocID = ID;
        }

        public void setHangSX(int ID, String Name, String Country)
        {
            txtTenHangSX.Text = Name;
            txtQuocGia.Text = Country;
            ctT.HangSXID = ID;
        }

        public void setHC(int ID)
        {
            HC.HoatChatID = ID;
            frmHamLuong fhl = new frmHamLuong();
            fhl.saveHL = new frmHamLuong.saveHamLuong(saveHamL);
            fhl.ShowDialog();
        }

        public void saveHamL(float HamLuong, String DonVi)
        {
            HC.HamLuong = HamLuong;
            HC.DonVi = DonVi;
            HC.getMaxID();
            HC.insertHoatChat();
            dgvDSHoatChat.DataSource = HoatChatThuoc.getDanhSachHoatChat(ctT.ID);
        }

       
    }
}
